package com.yun.zf.admin.controller;

import com.aliyun.oss.OSS;
import com.yun.zf.common.esIndex.*;
import com.yun.zf.system.utils.AliyunOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;

@RestController
@RequestMapping("/esIndex")
@Api(tags = "索引管理")
public class EsIndex {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private AliyunOssUtil ossUtil;

    @ApiOperation(value = "社区索引")
    @PostMapping("/createCommunityIndex")
    void  createCommunityIndex(){
        // 创建索引
        IndexOperations operations = elasticsearchRestTemplate.indexOps(CommunityIndexTemplate.class);
        if (operations.exists()){
            operations.delete();
        }
        operations.create();
        operations.putMapping(CommunityIndexTemplate.class);


    }

    @ApiOperation(value = "房源索引")
    @PostMapping("/createHouseIndex")
    void  createHouseIndex(){
        // 创建索引
        IndexOperations operations = elasticsearchRestTemplate.indexOps(HouseIndexTemplate.class);
        if (operations.exists()){
            operations.delete();
        }
        operations.create();
        operations.putMapping(HouseIndexTemplate.class);
    }

    /**
     * 创建出租房源索引
     */
    @ApiOperation(value = "租房索引")
    @PostMapping("/createHouseRentIndex")
    void  createHouseRentIndex(){
        // 创建索引
        IndexOperations operations = elasticsearchRestTemplate.indexOps(HouseRentIndexTemplate.class);
        if (operations.exists()){
            operations.delete();
        }
        operations.create();
        operations.putMapping(HouseIndexTemplate.class);
    }

    /**
     * 创建客源索引
     */
    @ApiOperation(value = "客源索引")
    @PostMapping("/contextLoads")
    void contextLoads() {
        // 创建索引
        IndexOperations operations = elasticsearchRestTemplate.indexOps(CustomerIndexTemplate.class);
        if (operations.exists()){
            operations.delete();
        }
        operations.create();
        operations.putMapping(CustomerIndexTemplate.class);
        System.out.println(operations.getMapping());
    }

    @ApiOperation(value = "订单索引")
    @PostMapping("/orderIndex")
    void  orderIndex(){
        // 创建索引
        IndexOperations operations = elasticsearchRestTemplate.indexOps(OrderIndexTemplate.class);
        if (operations.exists()){
            operations.delete();
        }
        operations.create();
        operations.putMapping(OrderIndexTemplate.class);


    }


    @ApiOperation(value = "新建bucketName")
    @PostMapping("/createBucketName")
    @PreAuthorize("@ss.hasAnyRoles('admin')")
    void  createBucketName(String bucketName ){
        try {
            OSS ossClient =  ossUtil.getOSSClient();
            ossClient.createBucket(bucketName);
        }catch (Exception e){

        }

    }

    @ApiOperation(value = "新建folder")
    @PostMapping("/createfolder")
    @PreAuthorize("@ss.hasAnyRoles('admin')")
    void  createfolder(String bucketName ,String folder ){
        try {
            OSS ossClient =  ossUtil.getOSSClient();
            ossClient.putObject(bucketName, folder, new ByteArrayInputStream(new byte[0]));
        }catch (Exception e){

        }

    }

}
